//
//  ViewController.swift
//  YouPassword
//
//  Created by 李冲 on 15/11/16.
//  Copyright © 2015年 Saye. All rights reserved.
//

import UIKit
import SnapKit
import ChameleonFramework

class ViewController: UIViewController {
    
    var secretKeyTextField: UITextField?
    var keywordTextField: UITextField?
    var resultTextField: UITextField?
    var generateButton: UIButton?
    var configButton: UIButton?

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
        YPGlobal.applyTheme(self)
        self.initSubViews()
        self.createConstraint()
    }
    
    override func viewWillAppear(animated: Bool) {
        super.viewWillAppear(animated)
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

    //MARK: init subviews
    func initSubViews() {
        let secretTextField = UITextField(frame: CGRectMake(50, 100, 200, 40))
        secretTextField.font = UIFont.systemFontOfSize(17)
        secretTextField.placeholder = "secret key"
        secretTextField.borderStyle = .RoundedRect
        secretTextField.secureTextEntry = true
        self.view.addSubview(secretTextField)
        self.secretKeyTextField = secretTextField
        
        let keywordTextField = UITextField(frame: CGRectMake(50, 150, 200, 40))
        keywordTextField.font = UIFont.systemFontOfSize(17)
        keywordTextField.placeholder = "密码关键词"
        keywordTextField.borderStyle = .RoundedRect
        keywordTextField.clearButtonMode = .WhileEditing
        self.view.addSubview(keywordTextField)
        self.keywordTextField = keywordTextField
        
        let resultTextField = UITextField(frame: CGRectMake(50, 200, 200, 40))
        resultTextField.font = UIFont.systemFontOfSize(17)
        resultTextField.borderStyle = .RoundedRect
        self.view.addSubview(resultTextField)
        self.resultTextField = resultTextField
        
        let generateButton = UIButton(frame: CGRectMake(50, 250, 100, 40))
        generateButton.setTitle("生成", forState: .Normal)
        generateButton.addTarget(self, action: Selector("generateAction:"), forControlEvents:.TouchUpInside)
        generateButton.layer.cornerRadius = 5
        generateButton.layer.masksToBounds = true
        self.view.addSubview(generateButton)
        self.generateButton = generateButton
        
        let configButton = UIButton(frame: CGRectMake(50, 300, 100, 40))
        configButton.setTitle("设置", forState: .Normal)
        configButton.addTarget(self, action: Selector("showConfig"), forControlEvents: .TouchUpInside)
        configButton.backgroundColor = UIColor.clearColor()
        configButton.setTitleColor(UIColor.flatWatermelonColor(), forState: .Normal)
        self.view.addSubview(configButton)
        self.configButton = configButton
    }
    
    func createConstraint() {
        self.secretKeyTextField?.snp_makeConstraints(closure: { (make) -> Void in
            make.top.equalTo(self.view).offset(100)
            make.centerX.equalTo(self.view)
            make.width.equalTo(300)
            make.height.equalTo(44)
        })
        
        self.keywordTextField?.snp_makeConstraints(closure: { (make) -> Void in
            make.top.equalTo(self.secretKeyTextField!.snp_bottom).offset(11)
            make.centerX.equalTo(self.view)
            make.width.equalTo(self.secretKeyTextField!)
            make.height.equalTo(self.secretKeyTextField!)
        })
        
        self.resultTextField?.snp_makeConstraints(closure: { (make) -> Void in
            make.top.equalTo(self.keywordTextField!.snp_bottom).offset(11)
            make.centerX.equalTo(self.view)
            make.width.equalTo(self.keywordTextField!)
            make.height.equalTo(self.keywordTextField!)
        })
        
        self.generateButton?.snp_makeConstraints(closure: { (make) -> Void in
            make.top.equalTo(self.resultTextField!.snp_bottom).offset(30)
            make.centerX.equalTo(self.view)
            make.width.equalTo(180)
            make.height.equalTo(50)
        })
        
        self.configButton?.snp_makeConstraints(closure: { (make) -> Void in
            make.top.equalTo(self.generateButton!.snp_bottom).offset(15)
            make.centerX.equalTo(self.generateButton!)
            make.width.equalTo(100)
            make.height.equalTo(30)
        })
    }
    
    //MARK: action
    func generateAction(sender: UIButton)
    {
        if let secretKey = secretKeyTextField?.text,
            let keyword = keywordTextField?.text
        {
            let config = YPGlobal.passwordConfig()
            let generator = YPPasswordGenerator(config: config)
            let result = generator.generatePassword(secretKey, withKeyword: keyword)
            resultTextField?.text = result
        }
    }
    
    func showConfig() {
        if let viewController =
            YPOptionTableViewController.createEmbedWithNavigation(self, withConfig: YPGlobal.passwordConfig())
        {
            self.presentViewController(viewController, animated: true, completion: nil)
        }
    }

}

extension ViewController: YPOptionDelegate {
    func optionTableViewController(viewController: YPOptionTableViewController, completeWithConfig config: YPPasswordConfig) {
        YPGlobal.setPasswordConfig(config)
    }
}

